package com.example.testcalendar;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CalendarContract.Calendars;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

	public static final String[] EVENT_PROJECTION = new String[] {
			Calendars._ID, // 0
			Calendars.ACCOUNT_NAME, // 1
			Calendars.ACCOUNT_TYPE, // 2
			Calendars.CALENDAR_DISPLAY_NAME // 3
	};

	private static final int PROJECTION_ID_INDEX = 0;
	private static final int PROJECTION_ACCOUNT_NAME_INDEX = 1;
	private static final int PROJECTION_ACCOUNT_TYPE = 2;
	private static final int PROJECTION_DISPLAY_NAME = 3;

	private int count = 0;
	private long calID = 0;
	private String accountName = null;
	private String accountType = null;
	private String displayName = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		TextView textView = (TextView) findViewById(R.id.text1);
		Button nextButton = (Button) findViewById(R.id.button1);

		Cursor cur = fetchCalendarId();

		count = cur.getCount();

		String myAcc = "Total No. of result: " + count;
		while (cur.moveToNext()) {
			calID = cur.getLong(PROJECTION_ID_INDEX);
			accountName = cur.getString(PROJECTION_ACCOUNT_NAME_INDEX);
			accountType = cur.getString(PROJECTION_ACCOUNT_TYPE);
			displayName = cur.getString(PROJECTION_DISPLAY_NAME);

			myAcc = myAcc + "\n\nCalendar ID: " + calID + "\nAccount Name: "
					+ accountName + "\nAccount Type: " + accountType
					+ "\nDisplay Name: " + displayName;
		}

		myAcc += "\n";

		textView.setText(myAcc);

		nextButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this,
						DialogTesting.class);
				startActivity(intent);

			}
		});

	}

	private Cursor fetchCalendarId() {

		Cursor cur = null;
		ContentResolver cr = getContentResolver();
		Uri uri = Calendars.CONTENT_URI;

		cur = cr.query(uri, EVENT_PROJECTION, null, null,
				"Calendars._ID asc limit 10");

		return cur;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
